package p3.cse;

import java.io.IOException;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * This activity occurs when a user selects an account button from the list of
 * accounts for editing. User has two options, either remove the account or
 * update the account. The default back button can be used to exit.
 * 
 * @see Unlocked
 * @see Filer
 */
public final class Update extends Activity {
	/**
	 * These are the buttons. updateBtn The "Update" button removeBtn The
	 * "Remove" button
	 */
	private Button updateBtn, removeBtn;

	/**
	 * This is the Filer.java class reference.
	 */
	private Filer filer;

	/**
	 * The original account name is set to this. Used for populating the fields
	 * for this activity view.
	 */
	private String acct;

	/**
	 * The onClick event for remove button event. Returns to the unlocked view
	 * on completion.
	 * 
	 * @see Unlocked.java
	 */
	private OnClickListener remove = new OnClickListener() {
		public void onClick(final View v) {
			filer = Filer.getInstance();

			// Determine the account name, remove it form filer rep and then
			// save changes.
			EditText acc = (EditText) findViewById(R.id.account_edittext);
			filer.removeEntry(acc.getText().toString());
			try {
				filer.writeFile();
			} catch (IOException e) {
				Log.e("ChangePassword", "Error writing password database", e);
			}

			/* Close the activity */
			finish();
		}
	};

	/**
	 * The onClick event for update button event. Returns to the unlocked view
	 * on completion.
	 * 
	 * @see Unlocked.java
	 */
	private OnClickListener update = new OnClickListener() {
		public void onClick(final View v) {
			// The input field values are stored here with corresponding values.
			String account, url, username, password;

			// get filer instance
			filer = Filer.getInstance();

			// Read the form elements and store values in corresponding strings.
			EditText acc = (EditText) findViewById(R.id.account_edittext);
			account = acc.getText().toString();

			EditText urledit = (EditText) findViewById(R.id.url_edittext);
			url = urledit.getText().toString();

			EditText user = (EditText) findViewById(R.id.username_edittext);
			username = user.getText().toString();

			EditText pass = (EditText) findViewById(R.id.password_edittext);
			password = pass.getText().toString();

			// Create a new entry with the updated values from the form.
			Entry temp = new Entry(account, url, username, password);
			filer.updateEntry(acct, temp);

			// change in account name and new account name already exists
			if (!acct.equals(account) && filer.checkAccountExists(account)) {

				TextView warning = (TextView) findViewById(R.id.message);
				warning.setText("Duplicate Account Name.");
				warning.setVisibility(1);
				return;
			}

			Log.d("update acct", account);
			Log.d("update url", url);
			Log.d("update uname", username);
			// Log.d("update pass", password);

			try {
				filer.writeFile();
			} catch (IOException e) {
				Log.e("ChangePassword", "Error writing password database", e);
			}

			finish();
		}
	};

	/**
	 * Default constructor called by android when this activity is first
	 * created. Binds the events to the form buttons and sets the value of acct.
	 * 
	 * {@inheritDoc}
	 * 
	 * @see Unlocked
	 * @see /res/layout/update.xml
	 */
	@Override
	public void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.update);

		this.updateBtn = (Button) this.findViewById(R.id.update_btn);
		this.updateBtn.setOnClickListener(update);

		this.removeBtn = (Button) this.findViewById(R.id.remove_btn);
		this.removeBtn.setOnClickListener(remove);

		filer = Filer.getInstance();

		// Extra parameter sent with the account name which was clicked to edit.
		Bundle extras = getIntent().getExtras();
		this.acct = extras.getString("entry_account");

		// Find the account entry and populate the form with corresponding
		// values
		Entry entry = filer.getEntry(this.acct);

		EditText acc = (EditText) findViewById(R.id.account_edittext);
		acc.setText(entry.account);

		EditText urledit = (EditText) findViewById(R.id.url_edittext);
		urledit.setText(entry.url);

		EditText user = (EditText) findViewById(R.id.username_edittext);
		user.setText(entry.username);

		EditText pass = (EditText) findViewById(R.id.password_edittext);
		pass.setText(entry.password);

		String securityCheck = Filer.checkStrength(entry.password);
		if (securityCheck != null) {
			TextView warning = (TextView) findViewById(R.id.Warning);
			warning.setText("Warning: The password in this is too weak and you should consider changing it.");
			warning.setVisibility(1);
		}
	}
}
